package com.freework.sys.permissions.system.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.freework.freedbm.dao.TotalResult;
import com.freework.freedbm.exception.DAOException;
import com.freework.sys.permissions.external.service.SessionInfo;
import com.freework.sys.permissions.system.dao.UserDao;
import com.freework.sys.permissions.system.vo.SysGroupVO;
import com.freework.sys.permissions.system.vo.UserVO;
import com.freework.util.FormatUtil;
import com.freework.util.StringUtils;
@Service("freework.sys.permissions.system.service.UserEditServiceImpi")
public class UserEditServiceImpi implements UserEditServiceI {
	@Resource
	UserDao dao=null;
	
	public void setDao(UserDao dao) {
		this.dao = dao;
	}

	public boolean addUser(UserVO user) throws DAOException {
		user.setCreatetime(FormatUtil.stringDateTime2());
		Integer Userid=SessionInfo.getUser().getUserid();
		user.setCreateuser(Userid);
		return dao.addUser(user);
	}

	public boolean addUserGroup(String[] groupid, Integer userid) {
		
		 dao.insertGroup(groupid, userid);
		 return true;
	}

	public boolean deleteUser(String[] ids) {
		
		 dao.deleteUser(ids);
		 return true;
	}

	public TotalResult<UserVO> queryPage(int start, int limit, UserVO dto) {
		
		return dao.queryPage(start, limit, dto);
	}

	public List<SysGroupVO> queryUserGroup(Boolean isSelect, Integer userid,Integer cruserID) {
		
		return dao.queryUserGroup(isSelect, userid,cruserID);
	}

	public boolean updateUser(UserVO user) throws DAOException {
		if(user.getUserpassword()!=null&&!user.getUserpassword().trim().equals(""))
			user.setUserpassword(StringUtils.password(user.getUsercode()+user.getUserpassword()));
		else{
			UserVO user2=(UserVO) dao.getObject(user);
			user.setUserpassword(user2.getUserpassword());
		}
		
		return dao.updatePermission(user, "usercode");
	}

}
